package com.xxxx.server.produce;

import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.Mail;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author : ymd
 * @Date : 2020/10/21 0021 15:55
 * @Version 1.0
 * @Description : 发消息类
 */
@Component
public class Sender {
    @Resource
    private RabbitTemplate template;
    @Resource
    private EmployeeMapper employeeMapper;

    public void send (Employee employee){
        //此处可做处理逻辑
        System.out.println(employee);
        Mail mail = HandleMail(employee);
        PushMessage(mail,employee.getId());
    }

    //处理信息
    private Mail HandleMail(Employee employee) {
        Mail mail=new Mail();
        //
        mail.setId(employee.getId());
        mail.setName(employee.getName());
        mail.setPhone(employee.getPhone());
        mail.setEmail(employee.getEmail());
        mail.setTitle("欢迎加入！");
        String content=null;
        if ("男".equals(employee.getGender())){
            content="欢迎"+employee.getName()+"先生使用云E办系统,您是本系统的第"+employee.getId()+"位用户,祝您生活愉快！";
        }else if("女".equals(employee.getGender())) {
            content="欢迎"+employee.getName()+"女士使用云E办系统,您是本系统的第"+employee.getId()+"位用户,祝您生活愉快！";
        }
        mail.setContent(content);
        return mail;
    }

    private void PushMessage(Mail mail, Integer id) {
        //设置传输消息为JSON格式
        template.setMessageConverter(new Jackson2JsonMessageConverter());
        template.convertAndSend("topicExchange", "topic.msg", mail);
//        for (int i=0;i<10000;i++){
            Employee employee = employeeMapper.selectById(id);
            mail.setId(employee.getId());
            template.convertAndSend("topicExchange", "topic.email",mail);
//        }
    }
}
